package main

// $ go mod init github.com/zhangxi/proto_serialize_demo
// $ ==> go mod init proto_serialize_demo
// $ go mod tidy  #==> 下载项目中引入的第三方包,
//
// $ cd D:\go_dev\go_path\src\zhangxicode\gomicro_demo\proto_serialize_demo\proto
// $ ls  #==> userinfo.proto
// $ protoc --go_out=./ *.proto
// D:\go_dev\go_path\src\zhangxicode\gomicro_demo\proto_serialize_demo\proto> go mod tidy
// $ go mod tidy
// 因为在  "\proto_serialize_demo\proto\userService\userinfo.pb.go" 文件中引入了这些包, 所以需要我们 $ go mod tidy
//  ####    import (
//  ####    	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
//  ####    	protoimpl "google.golang.org/protobuf/runtime/protoimpl"
//  ####    	reflect "reflect"
//  ####    	sync "sync"
//  ####    )
// 查看 go.mod 文件, 可以看到 "require google.golang.org/protobuf v1.28.1" 这一行内容,
//

import (
	"fmt"
	"proto_serialize_demo/proto/userService"

	"google.golang.org/protobuf/proto"
)

func main() {
	u := &userService.Userinfo{
		Username: "zhangsan",
		Age:      20,
		Hobby:    []string{"eat", "sleep", "swim"},
	}
	fmt.Println(u)
	fmt.Println(u.GetType())
	fmt.Println("Username:", u.GetUsername())
	fmt.Println("Hobby:", u.GetHobby())

	// protobuf 的序列化,
	data, err := proto.Marshal(u)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(data)

	// protobuf 的反序列化,
	//var user userService.Userinfo
	//proto.Unmarshal(data, &user)
	user := userService.Userinfo{}
	err = proto.Unmarshal(data, &user)
	fmt.Printf("%#v\n", user)
	fmt.Println("Username:", user.GetUsername())
	fmt.Println("Hobby:", user.GetHobby())
}
